package com.hotau.util;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.hotau.Executable;
import com.hotau.HotauActivity;
import com.hotau.R;

public class DialogUtil {

	public static void alertDialog(Context context, String alertInfo) {
		new AlertDialog.Builder(context)
				.setIcon(R.drawable.alert_dialog_icon)
				.setTitle(alertInfo)
				.setPositiveButton(context.getString(R.string.tx_btn_ok),
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
							}
						}).create().show();
	}

	/**
	 * 
	 * 默认msg sp为15
	 * 
	 * @param context
	 * @param title
	 * @param msg
	 */
	public static void showDialog(Context context, String title,
			final String msg) {
		showDialog(context, title, msg, 15);
	}

	public static void showDialog(Context context, String title,
			final String msg, final com.hotau.Executable okExcecutable) {
		showDialog(context, title, msg, 15, okExcecutable);
	}

	public static void showDialog(Context context, String title,
			final String msg, int msgSpSize) {
		showDialogAndPushFragment(context, title, msg, msgSpSize, "", null);
	}

	public static void showDialog(final Context context, String title,
			String msg, final com.hotau.Executable okExcecutable,
			final com.hotau.Executable cancelExcecutable) {
		final Dialog dialog = getDialog(context, R.layout.r_okcanceldialogview);
		((TextView) dialog.findViewById(R.id.dialog_title)).setText(title);
		((TextView) dialog.findViewById(R.id.dialog_message)).setText(msg);
		((TextView) dialog.findViewById(R.id.dialog_message)).setTextSize(
				TypedValue.COMPLEX_UNIT_SP, 15);
		((Button) dialog.findViewById(R.id.ok))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (okExcecutable != null) {
							okExcecutable.execute();
						}
						dialog.dismiss();
					}
				});
		((Button) dialog.findViewById(R.id.cancel))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (cancelExcecutable != null) {
							cancelExcecutable.execute();
						}
						dialog.dismiss();
					}
				});

		dialog.show();

	}

	public static void show3MsgDialog(final Context context, String msg1,
			final String msg2, String msg3,
			final com.hotau.Executable okExcecutable) {
		final Dialog dialog = getDialog(context, R.layout.r_updateokdialogview);
		((TextView) dialog.findViewById(R.id.dialog_msg1)).setText(msg1);
		((TextView) dialog.findViewById(R.id.dialog_msg2)).setText(msg2);
		((TextView) dialog.findViewById(R.id.dialog_msg3)).setText(msg3);
		((Button) dialog.findViewById(R.id.ok)).setText(context
				.getString(R.string.tx_btn_ok_space));
		((Button) dialog.findViewById(R.id.ok))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// write your code to do things after users clicks OK
						if (okExcecutable != null) {
							okExcecutable.execute();
						}
						dialog.dismiss();
					}
				});
		dialog.show();
	}

	public static void showDialog(final Context context, String title,
			final String msg, int msgSpSize,
			final com.hotau.Executable okExcecutable) {
		final Dialog dialog = getDialog(context, R.layout.r_okdialogview);
		((TextView) dialog.findViewById(R.id.dialog_title)).setText(title);
		((TextView) dialog.findViewById(R.id.dialog_message)).setText(msg);
		((TextView) dialog.findViewById(R.id.dialog_message)).setTextSize(
				TypedValue.COMPLEX_UNIT_SP, msgSpSize);
		((Button) dialog.findViewById(R.id.ok)).setText(context
				.getString(R.string.tx_btn_ok_space));
		((Button) dialog.findViewById(R.id.ok))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// write your code to do things after users clicks OK
						if (okExcecutable != null) {
							okExcecutable.execute();
						}
						dialog.dismiss();
					}
				});
		dialog.show();
	}

	public static Dialog getDialog(Context context, int layoutId) {
		Dialog dialog = new Dialog(context,
				android.R.style.Theme_Translucent_NoTitleBar);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(layoutId);
		return dialog;
	}

	public static void showDialogAndPushFragment(final Context context,
			String title, final String msg, int msgSpSize,
			final String fragmentTitle, final Fragment fragment) {

		Executable okExcecutable = new Executable() {
			@Override
			public void execute() {
				if (fragment != null) {
					String finalFragmentTitle = fragmentTitle == null ? ""
							: fragmentTitle;
					HotauActivity csmbpActivity = (HotauActivity) context;
					csmbpActivity.pushFragment(finalFragmentTitle, fragment);
				}
			}
		};

		showDialog(context, title, msg, msgSpSize, okExcecutable);
	}

	public static void showDialogPopFragment(final Context context,
			String title, final String msg, int msgSpSize) {
		final Dialog dialog = new Dialog(context,
				android.R.style.Theme_Translucent_NoTitleBar);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.r_okdialogview);
		((TextView) dialog.findViewById(R.id.dialog_title)).setText(title);
		((TextView) dialog.findViewById(R.id.dialog_message)).setText(msg);
		((TextView) dialog.findViewById(R.id.dialog_message)).setTextSize(
				TypedValue.COMPLEX_UNIT_SP, msgSpSize);
		((Button) dialog.findViewById(R.id.ok)).setText(context
				.getString(R.string.tx_btn_ok_space));
		((Button) dialog.findViewById(R.id.ok))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// write your code to do things after users clicks OK
						dialog.dismiss();
						HotauActivity csmbpActivity = (HotauActivity) context;
						csmbpActivity.popFragment();
					}
				});
		dialog.show();

	}

}
